package FixBrand;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Map;

public class Main {
	public static void main(String[] args) throws SQLException {
		Connection con=DAO.DbcpConnection.getConnection();
		con.setAutoCommit(false);
		Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		ResultSet rs=st.executeQuery("select * from all_detail_total");
		int k=0;
		while (rs.next()) {
			System.out.println(k++);
			String title=rs.getString("title");
			String ybrand=rs.getString("brand");
			String brand=getBrand(title,ybrand);
			if(brand!=null){
				rs.updateString("brand", brand);
				rs.updateRow();
			}
		}
		con.commit();
	}

	public static String getBrand(String title,String ybrand) {
		// TODO Auto-generated method stub
		if(ybrand==null){
			ybrand="";
		}
		if(title==null){
			title="";
		}
		
		Brand b=Brand.getInstance();
		Map<String, String> brandMap=b.brandMap;
		for (Iterator iterator = brandMap.keySet().iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			String brand=brandMap.get(key);
			if(title.contains(key)){
				return brand;
			}
			if(title.contains(brand)){
				return brand;
			}
			if(ybrand.contains(brand)){
				return brand;
			}
			if(ybrand.contains(key)){
				return brand;
			}
		}
		if(ybrand.equals("")){
			ybrand=null;
		}
		return ybrand;
	}
}
